<?php
/**
 * Created by PhpStorm.
 * User: wzy12
 * Date: 2018/9/23
 * Time: 11:40
 */

namespace app\admin\model;


use think\Model;
use app\admin\validate\GoodsSpec as Vali;

class GoodsSpec extends Model
{
    public function item(){
        return $this->hasMany('GoodsSpecItem', 'spec_id') -> field('item');
    }

    public function operation($data){
        // 规格数据转换数组
        $data['spec_value'] = trim($data['spec_value']);
        $temp = explode("\n", $data['spec_value']);
        foreach($temp as $k => $v){
            $arr[] = ['item' => $v];
        }

        $query = $this -> where('spec_name', $data['spec_name']) -> where('model_id', $data['model_id']);
        $action = false;
        if(isset($data['id'])){
            $action = true;

            $query -> where('id', '<>', $data['id']);
        }
        // 判断是否已有该规格
        $res = $query -> value('id');
        if($res){
            return echoArr(0, '该模型下的这个规格已存在');
        }

        // 清除规格值
        if(isset($data['id'])){
            model('GoodsSpecItem') -> whereIn('spec_id', $data['id']) -> delete();
        }

        // 验证
        $validate = new Vali();
        if(!$validate -> check($data)){
            return echoArr(0, $validate->getError());
        }

        $result = $this -> allowField(true) -> isUpdate($action) -> save($data);
        if(false === $result){
            return echoArr(0, $this -> getError());
        }else{
            if(!isset($data['id'])){
                $data['id'] = $this -> id;
            }

            // 新增规格
            $curr = $this -> find($data['id']);
            $curr->item()->saveAll($arr);

            return echoArr(1, '操作成功');
        }
    }

    public function resList($where){
        $query = $this -> where(1);

        // 筛选条件
        $is_where = $where;
        unset($is_where['page']);
        if($is_where) {
            if ($where) {
                // 查询模型
                if ($where['model_id']) {
                    $query->where('model_id', $where['model_id']);
                }
            }
        }

        // 规格
        $list = $query -> alias('s') -> join('goods_model m', 'm.id = s.model_id') -> field('s.*,m.model_name') -> paginate(10, false, ['query'=>request()->param()]);
        foreach($list as $k => $v){
            $list[$k]['spec_value'] = $v -> item;
            $str = '';
            foreach($list[$k]['spec_value'] as $key => $val){
                $temp = ', ';
                if($key == count($list[$k]['spec_value']) - 1){
                    $temp = '';
                }
                $str .= $val['item'] . $temp;
            }
            $list[$k]['spec_value'] = $str;
        }

        // 模型
        $model = model('GoodsModel') -> select();

        $arr['list'] = $list;
        $arr['model'] = $model;

        return $arr;
    }

    public function resFind($id){
        $result = $this -> find($id);
        $result['spec_value'] = $result -> item;
        $str = '';
        foreach($result['spec_value'] as $k => $v){
            $str .= $v['item'] . "\n";
        }

        $result['spec_value'] = trim($str);

        return $result;
    }

    public function del($data){
        // 验证
        $validate = new Vali();
        if(!$validate -> scene('del') -> check($data)){
            return echoArr(0, $validate->getError());
        }

        // 验证商品中是否包含此规格
        $is_spec = model('GoodsSpecItem') -> alias('gitem')
                -> join('GoodsSpecImg gimg', 'gitem.id = gimg.spec_item_id')
                -> whereIn('gitem.spec_id', $data['id'])
                -> value('gitem.id');
        if($is_spec){
            return echoArr(0, '请先去除拥有此规格的商品');
        }

        $result = $this -> destroy($data['id']);
        model('GoodsSpecItem') -> whereIn('spec_id', $data['id']) -> delete();

        if(false === $result){
            return echoArr(0, '操作失败');
        }else{
            return echoArr(1, '操作成功');
        }
    }
}